package com.baizhi.controller;


import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Video;
import com.baizhi.service.VideoService;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("video")
public class VideoController {

    private static final Logger log = LoggerFactory.getLogger(VideoController.class);
    @Resource
    VideoService videoService;

    @ResponseBody
    @RequestMapping("queryAllPage")
    public CommonQueryPageVO queryAllPage(@RequestBody PageDTO pageDTO) {

        log.info("当前页：{}", pageDTO.getPage());
        log.info("每页展示条数：{}", pageDTO.getPageSize());

        CommonQueryPageVO commonQueryPageVO = videoService.queryAllPages(pageDTO.getPage(), pageDTO.getPageSize());

        return commonQueryPageVO;
    }

    @ResponseBody
    @RequestMapping("delete")
    public CommonVO delete(@RequestBody Video video) {
        log.info("videos：{}", video);
        try {
            videoService.delete(video);
            return CommonVO.success("删除成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild("删除失败！！！");
        }
    }

    @ResponseBody
    @RequestMapping("update")
    public CommonVO update(@RequestBody Video video) {
        log.info("videos：{}", video);
        try {
            videoService.update(video);
            return CommonVO.success("修改成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild("修改失败！！！");
        }
    }

    @ResponseBody
    @RequestMapping("add")
    public CommonVO add(@RequestBody Video video) {
        log.info("videos：{}", video);
        try {
            videoService.add(video);
            return CommonVO.success("添加成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild("添加失败！！！");
        }
    }

    @ResponseBody
    @RequestMapping("uploadHeadImg")
    public HashMap<String, String> uploadHeadImg(MultipartFile videoFile){
        return videoService.uploadHeadImg(videoFile);
    }

}
